home *** CD-ROM | disk | FTP | other *** search
/ Freelog 115 / FreelogNo115-MaiJuin2013.iso / Internet / AvantBrowser / asetup.exe / _data / webkit / chrome_100_percent.pak / Unnamed File 000027.txt < prev    next >
Text File  |  2013-04-03  |  3KB  |  92 lines

  1. // Copyright (c) 2012 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4.  
  5. // Custom bindings for the Permissions API.
  6.  
  7. var chromeHidden = requireNative('chrome_hidden').GetChromeHidden();
  8. var sendRequest = require('sendRequest').sendRequest;
  9. var lastError = require('lastError');
  10.  
  11. // These custom bindings are only necessary because it is not currently
  12. // possible to have a union of types as the type of the items in an array.
  13. // Once that is fixed, this entire file should go away.
  14. // See,
  15. // https://code.google.com/p/chromium/issues/detail?id=162044
  16. // https://code.google.com/p/chromium/issues/detail?id=162042
  17. // TODO(bryeung): delete this file.
  18. chromeHidden.registerCustomHook('permissions', function(api) {
  19.   var apiFunctions = api.apiFunctions;
  20.  
  21.   function maybeConvertToObject(str) {
  22.     var parts = str.split('|');
  23.     if (parts.length != 2)
  24.       return str;
  25.  
  26.     var ret = {};
  27.     ret[parts[0]] = JSON.parse(parts[1]);
  28.     return ret;
  29.   }
  30.  
  31.   function convertObjectPermissionsToStrings() {
  32.     if (arguments.length < 1)
  33.       return arguments;
  34.  
  35.     var args = arguments[0].permissions;
  36.     if (!args)
  37.       return arguments;
  38.  
  39.     for (var i = 0; i < args.length; i += 1) {
  40.       if (typeof(args[i]) == 'object') {
  41.         var a = args[i];
  42.         var keys = Object.keys(a);
  43.         if (keys.length != 1) {
  44.           throw new Error("Too many keys in object-style permission.");
  45.         }
  46.         arguments[0].permissions[i] = keys[0] + '|' +
  47.             JSON.stringify(a[keys[0]]);
  48.       }
  49.     }
  50.  
  51.     return arguments;
  52.   }
  53.  
  54.   // Convert complex permissions to strings so they validate against the schema
  55.   apiFunctions.setUpdateArgumentsPreValidate(
  56.       'contains', convertObjectPermissionsToStrings);
  57.   apiFunctions.setUpdateArgumentsPreValidate(
  58.       'remove', convertObjectPermissionsToStrings);
  59.   apiFunctions.setUpdateArgumentsPreValidate(
  60.       'request', convertObjectPermissionsToStrings);
  61.  
  62.   // Convert complex permissions back to objects
  63.   apiFunctions.setCustomCallback('getAll',
  64.       function(name, request, response) {
  65.         for (var i = 0; i < response.permissions.length; i += 1) {
  66.           response.permissions[i] =
  67.               maybeConvertToObject(response.permissions[i]);
  68.         }
  69.  
  70.         // Since the schema says Permissions.permissions contains strings and
  71.         // not objects, validation will fail after the for-loop above.  This
  72.         // skips validation and calls the callback directly, then clears it so
  73.         // that handleResponse doesn't call it again.
  74.         if (request.callback)
  75.           request.callback.apply(request, [response]);
  76.         delete request.callback;
  77.       });
  78.  
  79.   // Also convert complex permissions back to objects for events.  The
  80.   // dispatchToListener call happens after argument validation, which works
  81.   // around the problem that Permissions.permissions is supposed to be a list
  82.   // of strings.
  83.   chrome.permissions.onAdded.dispatchToListener = function(callback, args) {
  84.     for (var i = 0; i < args[0].permissions.length; i += 1) {
  85.       args[0].permissions[i] = maybeConvertToObject(args[0].permissions[i]);
  86.     }
  87.     chrome.Event.prototype.dispatchToListener(callback, args);
  88.   };
  89.   chrome.permissions.onRemoved.dispatchToListener =
  90.       chrome.permissions.onAdded.dispatchToListener;
  91. });
  92.